﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WDTAsg2.DbBizObject;

namespace WDTAsg2.Admin
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                DataBindList();
            }
        }

        // Rebind data
        private void DataBindList()
        {
            HardwaresRUsDataContext db = new HardwaresRUsDataContext();
            this.lvProducts.DataSource = db.getAllProducts();
            this.lvProducts.DataBind();
        }

        protected void lvProducts_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            lvProducts.EditIndex = e.NewEditIndex;
            DataBindList();

        }

        protected void lvProducts_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            // Update item
            ListViewItem item = lvProducts.Items[e.ItemIndex];

            //Params
            int idParam = Convert.ToInt32(((Label)item.FindControl("ProductIdLabel")).Text);
            string titleParam = ((TextBox)item.FindControl("TitleTextBox")).Text;
            string tagLineParam = ((TextBox)item.FindControl("TagLineTextBox")).Text;
            string descriptionParam = ((TextBox)item.FindControl("DescriptionTextBox")).Text;
            int categoryIdParam = Convert.ToInt32(((DropDownList)item.FindControl("ddCategoryId")).SelectedValue);
            string imageUrlParam = ((TextBox)item.FindControl("ImageUrlTextBox")).Text;
            string keywordsParam = ((TextBox)item.FindControl("KeywordsTextBox")).Text;

            //Linq
            HardwaresRUsDataContext dc = new HardwaresRUsDataContext();

            dc.editProductById(idParam, titleParam, tagLineParam, descriptionParam, categoryIdParam, imageUrlParam, keywordsParam);

            this.lvProducts.EditIndex = -1;
            DataBindList();
        }

        protected void lvProducts_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            lvProducts.EditIndex = -1;
            DataBindList();
        }

        protected void lvProducts_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            //Inert item
            ListViewItem item = e.Item;
            //Params
            string titleParam = ((TextBox)item.FindControl("TitleTextBox")).Text;
            string tagLineParam = ((TextBox)item.FindControl("TagLineTextBox")).Text;
            string descriptionParam = ((TextBox)item.FindControl("DescriptionTextBox")).Text;
            int categoryIdParam = Convert.ToInt32(((DropDownList)item.FindControl("ddCategoryId")).SelectedValue);
            string imageUrlParam = ((TextBox)item.FindControl("ImageUrlTextBox")).Text;
            string keywordsParam = ((TextBox)item.FindControl("KeywordsTextBox")).Text;


            // Linq - insert
            HardwaresRUsDataContext dc = new HardwaresRUsDataContext();

            dc.insertProduct(titleParam, tagLineParam, descriptionParam, categoryIdParam, imageUrlParam, keywordsParam);
            DataBindList();
        }

        protected void lvProducts_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            // Get item
            ListViewItem item = lvProducts.Items[e.ItemIndex];

            int idParam = Convert.ToInt32(((Label)item.FindControl("ProductIdLabel")).Text);

            //Delete item
            HardwaresRUsDataContext dc = new HardwaresRUsDataContext();
            dc.deleteProductById(idParam);

            DataBindList();
        }
    }
}